//
//  ShanghaiNightsScrollView.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/1.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "ShanghaiNightsScrollView.h"

@interface ShanghaiNightsScrollView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) BaseButton *baseBtn;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *lbTitle;
@property (nonatomic, strong) UILabel *lbDetail;

@property (nonatomic, strong) UIButton *btnTitle;

@property (nonatomic, strong) NSMutableArray *modelArr;

@property (nonatomic, strong) UILabel *lbTopTitle;

@end


@implementation ShanghaiNightsScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self initUI];
    }
    return self;
}

- (void)initUI {
    [self addSubview:self.lbTopTitle];
    [self addSubview:self.scrollView];
}

- (void)actionBtn:(UIButton *)sender {
    if (self.block) {
        self.block(sender, self.modelArr[sender.tag - 100][@"linkPath"]);
    }
}


//加载网络数据
- (void)updateShanghaiNightsScrollViewWith:(NSMutableArray *)arr {
    self.modelArr = arr;
    [self initUI];
}

- (void)addBtn {
    for (int i = 0; i < self.modelArr.count; i++) {
        CGFloat weight = ADAPT_W(311);

        self.baseBtn = [[BaseButton alloc] initWithFrame:CGRectMake(0, 0, weight, ADAPT_W(316))];
        self.baseBtn.backgroundColor = [UIColor whiteColor];
        self.baseBtn.tag = 100 + i;
        [self.baseBtn addTarget:self action:@selector(actionBtn:) forControlEvents:UIControlEventTouchUpInside];
#pragma mark - 圆角阴影
        UIView *shadowView             = [UIView new];
        shadowView.layer.shadowColor   = UIColorFromRGB(0xD8D8D8).CGColor;
        shadowView.layer.shadowOffset  = CGSizeMake(5, 10);
        shadowView.layer.shadowOpacity = 1;
        shadowView.layer.shadowRadius  = 10;
        shadowView.frame               = CGRectMake(i * ADAPT_W(329), 5.5, weight, ADAPT_W(316));
        
        [shadowView addSubview:self.baseBtn];
        [_scrollView addSubview:shadowView];

        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, weight, ADAPT_W(227))];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:DSStringValue(self.modelArr[i][@"imgPath"])] placeholderImage:LHYPlaceholderImage];

        self.lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(ADAPT_W(12), ADAPT_W(15) + GetViewYH(self.imageView), weight - ADAPT_W(24), ADAPT_W(23))];
        self.lbDetail = [[UILabel alloc] initWithFrame:CGRectMake(ADAPT_W(12), ADAPT_W(6) + GetViewYH(self.lbTitle), weight - ADAPT_W(24), ADAPT_W(17))];
        [_baseBtn addSubview:self.imageView];
        [_baseBtn addSubview:self.lbTitle];
        [_baseBtn addSubview:self.lbDetail];
        self.lbTitle.text = [NSString stringWithFormat:@"%@", DSStringValue(self.modelArr[i][@"title"])];
        self.lbDetail.text = [NSString stringWithFormat:@"%@", DSStringValue(self.modelArr[i][@"fromName"])];
        self.lbTitle.font = FONT_BOLD(16);
        self.lbDetail.font = FONT_BOLD(12);
        self.lbDetail.textColor = UIColorFromRGB(0x555555);
    }
}

- (NSMutableArray *)modelArr {
    if (!_modelArr) {
        _modelArr = [[NSMutableArray alloc] init];
    }
    return _modelArr;
}

- (UILabel *)lbTopTitle {
    if (!_lbTopTitle) {
        _lbTopTitle = [[UILabel alloc] initWithFrame:CGRectMake(16, ADAPT_W(24), SCREEN_WIDTH - 32, ADAPT_W(31))];
        _lbTopTitle.text = @"夜上海专栏";
        _lbTopTitle.font = FONT_BOLD(22);
    }
    return _lbTopTitle;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(16, GetViewYH(self.lbTopTitle) + ADAPT_W(15), SCREEN_WIDTH - 16, ADAPT_W(400))];
        _scrollView.contentSize = CGSizeMake(ADAPT_W(329) * self.modelArr.count, 182);
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addBtn];
    }
    return _scrollView;
}

@end
